import {addAppointmentToOrder} from "../../ajax/module/userInfo.js"
import {adapter} from "../../utils/index.js"
import imgUrl from "../../images/util.js"

var app = getApp();

Page({
  data: {
    isAdapter:false,
    loginInfoStore:{},
    appointmentTime:{},
    appointEmp:{},
    orderUser:{},
    orderSku:[],
    storeIcon:imgUrl.storeIcon,
    appointmnetIcon:imgUrl.appointmnetIcon,
    serverIcon:imgUrl.serverIcon,
    appointmentEmpIcon1:imgUrl.appointmentEmpIcon1 ,
    shopIcon:imgUrl.shopIcon,

    id:""
  },
  onShow(){
    
      
  },
  
  onLoad(option) {
    adapter().then(res=>{
      console.log(res)
      this.setData({
        isAdapter:res
      })
    })
    console.log(option)
    if(option.id == 1){
      // 更新暂存 里的 服务时长 和 关联的家政师
       this.setData({
         id:1,
         appointmentTime:app.globalData.appointmentTime,
         appointEmp:app.globalData.appointEmp,

        // 原订单列表 已经有的部分数据
         loginInfoStore:app.globalData.loginInfoStore,
         orderUser:app.globalData.orderUser,
         orderSku:app.globalData.orderSku
       })
    }else{
        // getApp().globalData.name = "王二麻子"   
    console.log(app.globalData.loginInfoStore)
    console.log(app.globalData.appointmentTime)
    console.log(app.globalData.appointEmp)
    console.log(app.globalData.orderUser)
    console.log(app.globalData.orderSku)
    this.setData({
      id:"",
      appointmentTime:app.globalData.appointmentTime,
      appointEmp:app.globalData.appointEmp,
      loginInfoStore:app.globalData.loginInfoStore,
      orderUser:app.globalData.orderUser,
      orderSku:app.globalData.orderSku

    })
    }

   
    
   
  },
  goBack(){
    wx.navigateBack()
  },
  sureJump(){
    wx.navigateTo({
      url: '../appointment/appointment',
    })
  },
  submitAppointment(){
    let {text} =app.globalData.appointmentTime
    let {detailAddress}=app.globalData.orderUser
    let temp1=new Date((app.globalData.appointmentTime.startTime+"").replace(/-/g, '/')).getTime()
    let temp2=Number(temp1) + (Number(app.globalData.appointmentTime.timeLength.split("小")[0])*60*60*1000) +(Number(app.globalData.appointmentTime.timeLength.split("时")[1].slice(0,1) )*60*1000) 
    // let {startTime,timeLength}=app.globalData.appointmentTime
    console.log("bb",new Date((app.globalData.appointmentTime.startTime+"").replace(/-/g, '/')))
    console.log("aa",temp2)
     
    function add0(m){return m<10?'0'+m:m }
function format(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm);
}

    // "address": "",
   
    // "employeeId": 0,
    // "endTime": "",
    // "latitude": 0,
    // "longitude": 0,
    // "orderId": 0,
    // "startTime": "",
   
    let address=app.globalData.loginInfoStore.address
    let orderId=app.globalData.other.orderId
    let longitude=""
    let latitude=""
    let employeeId=app.globalData.appointEmp.employeeId
    let startTime=format(temp1)
    let endTime=format(temp2)
    let isAssign= app.globalData.other.isAssign||""
    console.log("isAKK",isAssign)

    console.log("yes")

    let data={
      "address": address,
      "taskId":"",
      "employeeId": employeeId,
      "endTime": endTime+":00",
      "latitude": 0,
      "longitude": 0,
      "orderId": Number(orderId+""),
      "startTime": startTime+":00",
      
    }
    console.log("data",data)
    addAppointmentToOrder(data,isAssign).then(res=>{
         console.log("fuck",res)
         wx.showToast({
           title: '预约成功',
         })

        //  清空暂存
        app.globalData.newAddStaff={}
        // app.globalData.loginInfoStore={}
    app.globalData.appointmentTime={}
    app.globalData.appointEmp={}
    app.globalData.orderUser={}
    app.globalData.orderSku={}
    app.globalData.other={}

    let param=JSON.stringify({text,detailAddress})
    
    
    wx.navigateTo({
      url: '../appointmentSuccess/appointmentSuccess?param='+param,
    })


    })

   



   
  }
  
})
